package com.bigstar.lolkor;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Masteries_Utility extends Activity implements OnClickListener{
	public static final String[] title = {
		"Summoner's Insight(소환사의 통찰력)", "Good Hands(도움의 손길)", "Expanded Mind(정신 확장)", "Improved Recall(귀환 강화)",
		"Swiftness(신속)", "Meditation(명상)", "Scout(정찰)",
		"Greed(탐욕)", "Transmutation(변성)", "Runic Affinity(룬 친화력)",
		"Wealth(부유함)", "Awareness(자각)", "Sage(현자)",
		"Strength of Spirit(영혼의 힘)", "Intelligence(지능)",
		"Mastermind(깨달은 자)",
	};
	public static final String[] text = {
		"다음 소환사 주문의 성능이 향상됩니다:\r\n순간이동 : 시전 시간이 0.5초 감소합니다.\r\n진급 : 진급된 미니언의 방어 관련 능력치가 15% 증가합니다.\r\n점멸 : 재사용 대기시간이 15초 감소합니다.\r\n총명 : 마나 회복량이 20% 증가합니다.\r\n천리안 : 지속 시간이 2초 증가합니다.",
		"사망시 재소환 시간이 num% 감소합니다.",
		"레벨당 마나 +num\r\n(챔피언 18 레벨 달성시 마나 +72/144/216)\r\n또는 기력 +4/7/10",
		"귀환 시전 시간이 num초 감소합니다. 상급 귀환은 0.5초 감소합니다.",
		"이동 속도 +num%",
		"5초당 마나 재생 +num",
		"와드의 시야가 num% 증가합니다",
		"10초마다 추가로 num골드를 얻습니다.",
		"주문 흡혈 +num%",
		"중립 몬스터, 신전, 유물, 퀘스트로 얻는 이로운 효과의 지속시간이 num% 증가합니다.",
		"시작 시 추가로 num골드를 얻습니다.",
		"경험치 획득량이 num% 증가합니다.",
		"챔피언 킬, 어시스트시 경험치 num을 추가로 얻습니다.\r\n(수정의 상처에서는 효과 50% 감소)",
		"최대 마나의 num%만큼 체력 재생 증가",
		"재사용 대기시간 감소 +num%",
		"소환사 주문 재사용 대기시간이 num% 감소합니다."
	};
	
	public static final int max_point[] = { 1,3,3,1,4,3,1,4,3,1,2,4,1,3,3,1 };
	public static final String[][] level = {
		{"0"},
		{"4","7","10"},
		{"4","8","12"},
		{"1"},
		{"0.5","1","1.5","2"},
		{"1","2","3"},
		{"5"},
		{"0.5","1","1.5","2"},
		{"1","2","3"},
		{"20"},
		{"20","40"},
		{"1.25","2.5","3.75","5"},
		{"40"},
		{"0.4","0.7","1"},
		{"2","4","6"},
		{"15"}
	};
	public int spellid = 0;
	public static Activity context;
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState); 
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
		setContentView(R.layout.masteries_utility); 
		context = this;
 		if(getIntent().getExtras() != null){
 			ImageView iv = null;
 			for(int i = 0; i < 16; i++){
 				iv = (ImageView)findViewById(R.id.u_00+i);
 				iv.setOnClickListener(this);
 			}
 			TextView tv = (TextView)findViewById(R.id.b_utility);
			tv.setText("보조(UTILITY):  "+Builder.getMasteriesPoint(2));
			imageChange();
 		}else{
 			ImageView iv = null;
 			for(int i = 0; i < 16; i++){
 				iv = (ImageView)findViewById(R.id.u_00+i);
 				iv.setOnClickListener(this);
 			}
 		}
	}
	
    @Override
    protected void onResume() {
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
    	super.onResume();
 		if(getIntent().getExtras() != null){
 			ImageView iv = null;
 			for(int i = 0; i < 16; i++){
 				iv = (ImageView)findViewById(R.id.u_00+i);
 				iv.setOnClickListener(this);
 			}
 			TextView tv = (TextView)findViewById(R.id.b_utility);
			tv.setText("보조(UTILITY):  "+Builder.getMasteriesPoint(2));
			imageChange();
 		}
    }
	
	public static void imageChange(){
		ImageView iv = null;
		Bitmap bitmap = null;
		TextView tv = null;
		tv = (TextView)context.findViewById(R.id.b_utility);
		tv.setText("보조(UTILITY):  "+Builder.getMasteriesPoint(2));
		for(int i = 0; i < 16; i++){
			tv = (TextView)context.findViewById(R.id.u_text_00+i);
			tv.setText(" "+Builder.masteries[2][i]+"/"+max_point[i]);
			switch(i){
			case 4: 
			case 6:
				if(Builder.getMasteriesPoint(2) < 4){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 5:
				if(Builder.masteries[2][2] < 3){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					
					iv = (ImageView)context.findViewById(R.id.m_p_00);
					iv.clearColorFilter();
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
					iv.setImageBitmap(bitmap);
					continue;
				}
				if(Builder.getMasteriesPoint(2) < 4){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				iv = (ImageView)context.findViewById(R.id.m_p_00);
				iv.setColorFilter(Color.GREEN, PorterDuff.Mode.DARKEN);
				bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
				iv.setImageBitmap(bitmap);
				break;
			case 7:
			case 8:
			case 9:
				if(Builder.getMasteriesPoint(2) < 8){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 10:
				if(Builder.masteries[2][7] < 4){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					
					iv = (ImageView)context.findViewById(R.id.m_p_01);
					iv.clearColorFilter();
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
					iv.setImageBitmap(bitmap);
					continue;
				}
				if(Builder.getMasteriesPoint(2) < 12){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				iv = (ImageView)context.findViewById(R.id.m_p_01);
				iv.setColorFilter(Color.GREEN, PorterDuff.Mode.DARKEN);
				bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
				iv.setImageBitmap(bitmap);
				break;
			case 11:
			case 12:
				if(Builder.getMasteriesPoint(2) < 12){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 13:
			case 14:
				if(Builder.getMasteriesPoint(2) < 16){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 15:
				if(Builder.getMasteriesPoint(2) < 20){
					iv = (ImageView)context.findViewById(R.id.u_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			}
			iv = (ImageView)context.findViewById(R.id.u_00+i);
			iv.clearColorFilter();
			bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.u_00+i);
			iv.setImageBitmap(bitmap);
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.b_champ_m_minus:
			if(Builder.masteries[2][spellid] > 0){
				Builder.masteries[2][spellid]--;
				set();
			}
			break;
		case R.id.b_champ_m_plus:
			if(Builder.getMasteriesCount() >= 30){
				Toast.makeText(this, "포인트 30 다씀", Toast.LENGTH_LONG).show();
				return;
			}
			switch(spellid){
			case 4: 
			case 6:
				if(Builder.getMasteriesPoint(2) < 4){
					return;
				}
				break;
			case 5:
				if(Builder.masteries[2][2] < 3){
					return;
				}
				if(Builder.getMasteriesPoint(2) < 4){
					return;
				}
				break;
			case 7: 
			case 8:
			case 9:
				if(Builder.getMasteriesPoint(2) < 8){
					return;
				}
				break;
			case 10:
				if(Builder.masteries[2][7] < 4){
					return;
				}
				if(Builder.getMasteriesPoint(2) < 12){
					return;
				}
				break;
			case 11:
			case 12:
				if(Builder.getMasteriesPoint(2) < 12){
					return;
				}
				break;
			case 13:
			case 14:
				if(Builder.getMasteriesPoint(2) < 16){
					return;
				}
				break;
			case 15:
				if(Builder.getMasteriesPoint(2) < 20){
					return;
				}
				break;
			}
			if(Builder.masteries[2][spellid] < max_point[spellid]){
				Builder.masteries[2][spellid]++;
				set();
			}
			break;
		default:
			dialog(v.getId() - R.id.u_00);
			break;
		}
	}
	
	public void set(){
		TextView tv = (TextView)findViewById(R.id.b_utility);
		tv.setText("보조(UTILITY):  "+Builder.getMasteriesPoint(2));
		tv = (TextView)findViewById(R.id.u_text_00+spellid);
		tv.setText(" "+Builder.masteries[2][spellid]+"/"+max_point[spellid]);
		tv = (TextView) layout.findViewById(R.id.b_champ_m_text);
		tv.setText("단계: "+Builder.masteries[2][spellid]+"/"+max_point[spellid]+"\r\n\r\n"+text[spellid]);
		String limit = "";
		switch(spellid){
		case 4:
		case 6: limit = "\r\nUtility 4포인트 필요\r\n"; break;
		case 5: limit = "\r\nUtility 4포인트 필요\r\nExpanded Mind 3포인트 필요\r\n"; break;
		case 7:
		case 8:
		case 9: limit = "\r\nUtility 8포인트 필요\r\n"; break;
		case 10: limit = "\r\nUtility 12포인트 필요\r\nGreed 3포인트 필요\r\n"; break;
		case 11:
		case 12: limit = "\r\nUtility 12포인트 필요\r\n"; break;
		case 13: 
		case 14: limit = "\r\nUtility 16포인트 필요\r\n"; break;
		case 15: limit = "\r\nUtility 20포인트 필요\r\n"; break;
		}
		if(Builder.masteries[2][spellid] >= max_point[spellid]){
			String t = text[spellid];
			t = t.replaceAll("num", level[spellid][Builder.masteries[2][spellid]-1]);
			tv.setText("단계: "+Builder.masteries[2][spellid]+"/"+max_point[spellid]+"\r\n"+limit+"\r\n"+t);	
		}else if(Builder.masteries[2][spellid] == 0){
			String t = text[spellid];
			t = t.replaceAll("num", level[spellid][Builder.masteries[2][spellid]]);
			tv.setText("단계: "+Builder.masteries[2][spellid]+"/"+max_point[spellid]+"\r\n"+limit+"\r\n"+t);	
		}else{
			String t = text[spellid];
			t = t.replaceAll("num", level[spellid][Builder.masteries[2][spellid]-1]);
			String next = text[spellid];
			next = next.replaceAll("num", level[spellid][Builder.masteries[2][spellid]]);
			tv.setText("단계: "+Builder.masteries[2][spellid]+"/"+max_point[spellid]+"\r\n"+limit+"\r\n"+t
			          +"\r\n\r\n다음 단계:\r\n"+next);	
		}
	}
	
	View layout;
	
	private void dialog(int id) {  
 		if(getIntent().getExtras() != null){
 			spellid = id;
 			LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
 			layout = inflater.inflate(R.layout.build_masteries,(ViewGroup) findViewById(R.id.b_champ_m));
 			set();
 			Button bt = (Button)layout.findViewById(R.id.b_champ_m_minus);
 			bt.setOnClickListener(this);
 			bt = (Button)layout.findViewById(R.id.b_champ_m_plus);
 			bt.setOnClickListener(this);
 			AlertDialog.Builder alt_bld = new AlertDialog.Builder(getParent()); 
 			alt_bld
 			.setView(layout)
 			.setCancelable(false).setNegativeButton("닫기",  
 					new DialogInterface.OnClickListener() {
 				public void onClick(DialogInterface dialog, int id) {
 					imageChange();
 					dialog.cancel();
 				}});  
 			AlertDialog alert = alt_bld.create();  
 				alert.setTitle(title[id]);
 				alert.setIcon(R.drawable.u_00+id);
 				alert.show(); 
 		}else{
			String num = "";
			String t = text[id];
			for(int i = 0; i < level[id].length; i++){
				num = num+level[id][i];
				if(level[id].length == 1){
				}else if(i+1 < level[id].length){
					num = num+"/";
				}
			}
			t = t.replaceAll("num", num);
			TextView tv = new TextView(this);
			tv.setText(t);
			tv.setTextSize(14);
 			AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); 
 			alt_bld.setView(tv).setNegativeButton("닫기",  
 					new DialogInterface.OnClickListener() {
 				public void onClick(DialogInterface dialog, int id) {
 					dialog.cancel();
 				}});  
 			AlertDialog alert = alt_bld.create();  
 				alert.setTitle(title[id]);
 				alert.setIcon(R.drawable.u_00+id);
 				alert.show(); 	
 		}
	}
}